package com.nut.article.config;

import com.nut.article.handler.*;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static me.chanjar.weixin.common.api.WxConsts.EventType;
import static me.chanjar.weixin.common.api.WxConsts.EventType.*;
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT;
import static me.chanjar.weixin.mp.constant.WxMpEventConstants.CustomerService.KF_CREATE_SESSION;

@RequiredArgsConstructor
@Configuration
public class WxMpMessageRouterConfiguration {

    private final LogHandler logHandler;
    private final NullHandler nullHandler;
    private final KfSessionHandler kfSessionHandler;
    private final MenuHandler menuHandler;
    private final MsgHandler msgHandler;
    private final ScanHandler scanHandler;
    private final UnsubscribeHandler unsubscribeHandler;
    private final SubscribeHandler subscribeHandler;
//    private final PersonalAccountMsgHandler personalSubscriptionAccountMsgHandler;
    
    
//    private final String personalMPCategory = "personal";
//    private final String bussinessMpCategory = "business";
//    @Value("${wx.mp.category:Business}")
//    private String mPCategory;
    
    @Bean
    public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

        // 记录所有事件的日志 （异步执行）
        newRouter.rule().handler(this.logHandler).next();

        // 接收客服会话管理事件
        newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION).handler(this.kfSessionHandler).end();
        // 自定义菜单事件
        newRouter.rule().async(false).msgType(EVENT).event(EventType.CLICK).handler(this.menuHandler).end();
        // 关注事件
        newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
        // 取消关注事件
        newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
        //扫描带参二维码事件
        newRouter.rule().async(false).msgType(EVENT).event(SCAN).handler(this.scanHandler).end();
        // 其他事件
        newRouter.rule().async(false).msgType(EVENT).handler(this.nullHandler).end();
        //modify start:little-tiger,for personal mp
        // 默认 ，可以所有操作在这里(msgHandler)新增 --------------
//        if(personalMPCategory.equalsIgnoreCase(mPCategory)) {
//        	newRouter.rule().async(false).handler(this.personalSubscriptionAccountMsgHandler).end();
//        }
//        else {
//        	//bussin mp
//        	 newRouter.rule().async(false).handler(this.msgHandler).end();
//        }

        newRouter.rule().async(false).handler(this.msgHandler).end();
        
        return newRouter;
    }
}
